package com.maila.xiaoaoschool.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.activity.ShopActivity;

import com.maila.xiaoaoschool.entity.SaleGoods;
import com.maila.xiaoaoschool.util.Constants;
import com.maila.xiaoaoschool.util.NullPointUtils;
import com.maila.xiaoaoschool.util.StartActivityUtils;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 折扣优惠商品
 * Created by Administrator on 2017/9/26.
 */

public class SaleFoodAdapter extends BaseAdapter {
    Context context;
    List<SaleGoods.DataBean> mdata;

    public SaleFoodAdapter(Context context) {
        this.context = context;
        mdata=new ArrayList<>();
    }

    public List<SaleGoods.DataBean> getMdata() {
        return mdata;
    }

       public void setMdata(List<SaleGoods.DataBean> mdata) {
        this.mdata = mdata;
        notifyDataSetChanged();
    }

    public void addMdata(List<SaleGoods.DataBean> mdata) {
        this.mdata = mdata;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_sale, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
            AutoUtils.autoSize(view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        Glide.with(context).load(Uri.parse(Constants.URL_ROOT_XIAOAO+mdata.get(i).getImg())).into(holder.ivShop);
        holder.tvShopName.setText(mdata.get(i).getName());
        holder.tvXiaoliang.setText("月售："+mdata.get(i).getCount()+"份");
        if(!NullPointUtils.checknull(mdata.get(i).getCxcost())){
            holder.tvYuan.setText("￥"+mdata.get(i).getCost());
            holder.tvYuan.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
            holder.tvFee.setText("￥"+mdata.get(i).getCxcost());
        }else {
            holder.tvFee.setText("￥"+mdata.get(i).getCost());
            holder.tvYuan.setVisibility(View.GONE);
        }

        //返券
        if(NullPointUtils.checkzero(mdata.get(i).getFanquan())){
            holder.llFanquan.setVisibility(View.GONE);
        }else {
            holder.llFanquan.setVisibility(View.VISIBLE);
            holder.tvFanquan.setText(mdata.get(i).getFanquan());
        }
        //今日特价
        if(NullPointUtils.checkzero(mdata.get(i).getTejia())){
            holder.llTejia.setVisibility(View.GONE);
        }else {
            holder.llTejia.setVisibility(View.VISIBLE);
            holder.tvTejia.setText(mdata.get(i).getTejia());
        }
//        满减
        if(NullPointUtils.checkzero(mdata.get(i).getManjian())){
            holder.llManjian.setVisibility(View.GONE);
        }else {
            holder.llManjian.setVisibility(View.VISIBLE);
            holder.tvManjian.setText(mdata.get(i).getManjian());
        }

//首单
        if(NullPointUtils.checkzero(mdata.get(i).getShoudan())){
            holder.llShoudan.setVisibility(View.GONE);
        }else {
            holder.llShoudan.setVisibility(View.VISIBLE);
        }

//        折扣
        if(NullPointUtils.checkzero(mdata.get(i).getZhekou())){
            holder.llZhekou.setVisibility(View.GONE);
        }else {
            holder.llZhekou.setVisibility(View.VISIBLE);
            holder.tvZhekou.setText(mdata.get(i).getZhekou());
        }





        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO: 2018/3/1  跑腿费
                StartActivityUtils.startToShop(context, ShopActivity.class,mdata.get(i).getId(),mdata.get(i).getName(),mdata.get(i).getBagcost());
            }
        });
        return view;
    }


    static class ViewHolder {
        @BindView(R.id.iv_shop)
        ImageView ivShop;
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.tv_xiaoliang)
        TextView tvXiaoliang;
        @BindView(R.id.tv_fee)
        TextView tvFee;
        @BindView(R.id.tv_yuan)
        TextView tvYuan;
        @BindView(R.id.tv_manjian)
        TextView tvManjian;
        @BindView(R.id.ll_manjian)
        AutoLinearLayout llManjian;
        @BindView(R.id.tv_shoudan)
        TextView tvShoudan;
        @BindView(R.id.ll_shoudan)
        AutoLinearLayout llShoudan;
        @BindView(R.id.tv_zhekou)
        TextView tvZhekou;
        @BindView(R.id.ll_zhekou)
        AutoLinearLayout llZhekou;
        @BindView(R.id.tv_tejia)
        TextView tvTejia;
        @BindView(R.id.ll_tejia)
        AutoLinearLayout llTejia;
        @BindView(R.id.tv_fanquan)
        TextView tvFanquan;
        @BindView(R.id.ll_fanquan)
        AutoLinearLayout llFanquan;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
